<?php

class App_settingApp extends BackendApp {

    var $_mall_config_mod;

    function __construct() {
        $this->App_settingApp();
    }

    function App_settingApp() {
        parent::BackendApp();
        $_POST = stripslashes_deep($_POST);
        $this->_mall_config_mod = & m('mall_config');
    }

    /*
     * 基本设置同步
     */
    function base_setting() {
        $model_setting = &af('settings');
        $setting = $model_setting->getAll(); //载入系统设置数据
        if (!IS_POST) {
            $this->assign('setting', $setting);
            $this->display('app_setting.base_information.html');
        } else {
            
        }
    }
    

    function integral_setting() {
        $model_setting = &af('settings');
        $setting = $model_setting->getAll(); //载入系统设置数据

        if (!IS_POST) {
            $this->assign('setting', $setting);
            $this->display('app_setting.integral_setting.html');
        } else {
            
            $data['integral_enabled'] = $_POST['integral_enabled']; #是否开启
            $data['integral_reg'] = empty($_POST['integral_reg']) ? 0 : intval($_POST['integral_reg']);        #注册获得积分
            $data['integral_login'] = empty($_POST['integral_login']) ? 0 : intval($_POST['integral_login']);  #登录获得积分
            $data['integral_recom'] = empty($_POST['integral_recom']) ? 0 : intval($_POST['integral_recom']);  #推荐获得积分
            $data['integral_buy'] = empty($_POST['integral_buy']) ? 0 : round($_POST['integral_buy'], 2);   #购买获得积分
            $data['integral_seller'] = empty($_POST['integral_seller']) ? 0 : round($_POST['integral_seller'], 2);   #用户购买抵扣积分 比例

            if ($data['integral_enabled'] != $setting['integral_enabled']) {
                $this->_mall_config_mod->edit('code="integral_enabled"', array('value' => $data['integral_enabled']));
            }
            if ($data['integral_reg'] != $setting['integral_reg']) {
                $this->_mall_config_mod->edit('code="integral_reg"', array('value' => $data['integral_reg']));
            }
            if ($data['integral_login'] != $setting['integral_login']) {
                $this->_mall_config_mod->edit('code="integral_login"', array('value' => $data['integral_login']));
            }
            if ($data['integral_recom'] != $setting['integral_recom']) {
                $this->_mall_config_mod->edit('code="integral_recom"', array('value' => $data['integral_recom']));
            }
            if ($data['integral_buy'] != $setting['integral_buy']) {
                $this->_mall_config_mod->edit('code="integral_buy"', array('value' => $data['integral_buy']));
            }
            if ($data['integral_seller'] != $setting['integral_seller']) {
                $this->_mall_config_mod->edit('code="integral_seller"', array('value' => $data['integral_seller']));
            }
            
            $model_setting->setAll($data);
            $this->show_message('edit_successed');
        }
    }

    function msg_setting() {
        $model_setting = &af('settings');
        $setting = $model_setting->getAll(); //载入系统设置数据

        if (!IS_POST) {
            $this->assign('setting', $setting);
            $this->display('app_setting.msg_setting.html');
        } else {
            $data['msg_enabled'] = $_POST['msg_enabled'];
            $data['msg_pid'] = trim($_POST['msg_pid']);
            $data['msg_key'] = trim($_POST['msg_key']);

            
            $this->_mall_config_mod->edit('code="msg_enabled"', array('value' => $data['msg_enabled']));
            
            $this->_mall_config_mod->edit('code="msg_pid"', array('value' => $data['msg_pid']));
            
            $this->_mall_config_mod->edit('code="msg_key"', array('value' => $data['msg_key']));
            

            $model_setting->setAll($data);
            $this->show_message('edit_successed');
        }
    }

    function tuijian_setting() {
        $model_setting = &af('settings');
        $setting = $model_setting->getAll(); //载入系统设置数据
        if (!IS_POST) {
            $this->assign('setting', $setting);
            $this->display('app_setting.tuijian_setting.html');
        } else {

            $data['tuijian_seller_status'] = $_POST['tuijian_seller_status'];
            $data['tuijian_seller_ratio1'] = trim($_POST['tuijian_seller_ratio1']);
            $data['tuijian_seller_ratio2'] = trim($_POST['tuijian_seller_ratio2']);
            $data['tuijian_seller_ratio3'] = trim($_POST['tuijian_seller_ratio3']);

            $data['tuijian_buyer_status'] = $_POST['tuijian_buyer_status'];
            $data['tuijian_buyer_ratio1'] = trim($_POST['tuijian_buyer_ratio1']);
            $data['tuijian_buyer_ratio2'] = trim($_POST['tuijian_buyer_ratio2']);
            $data['tuijian_buyer_ratio3'] = trim($_POST['tuijian_buyer_ratio3']);

            if ($data['tuijian_seller_status'] != $setting['tuijian_seller_status']) {
                $this->_mall_config_mod->edit('code="tuijian_seller_status"', array('value' => $data['tuijian_seller_status']));
            }
            if ($data['tuijian_seller_ratio1'] != $setting['tuijian_seller_ratio1']) {
                $this->_mall_config_mod->edit('code="tuijian_seller_ratio1"', array('value' => $data['tuijian_seller_ratio1']));
            }
            if ($data['tuijian_seller_ratio2'] != $setting['tuijian_seller_ratio2']) {
                $this->_mall_config_mod->edit('code="tuijian_seller_ratio2"', array('value' => $data['tuijian_seller_ratio2']));
            }
            if ($data['tuijian_seller_ratio3'] != $setting['tuijian_seller_ratio3']) {
                $this->_mall_config_mod->edit('code="tuijian_seller_ratio3"', array('value' => $data['tuijian_seller_ratio3']));
            }
            if ($data['tuijian_buyer_status'] != $setting['tuijian_buyer_status']) {
                $this->_mall_config_mod->edit('code="tuijian_buyer_status"', array('value' => $data['tuijian_buyer_status']));
            }
            if ($data['tuijian_buyer_ratio1'] != $setting['tuijian_buyer_ratio1']) {
                $this->_mall_config_mod->edit('code="tuijian_buyer_ratio1"', array('value' => $data['tuijian_buyer_ratio1']));
            }
            if ($data['tuijian_buyer_ratio2'] != $setting['tuijian_buyer_ratio2']) {
                $this->_mall_config_mod->edit('code="tuijian_buyer_ratio2"', array('value' => $data['tuijian_buyer_ratio2']));
            }
            if ($data['tuijian_buyer_ratio3'] != $setting['tuijian_buyer_ratio3']) {
                $this->_mall_config_mod->edit('code="tuijian_buyer_ratio3"', array('value' => $data['tuijian_buyer_ratio3']));
            }
            $model_setting->setAll($data);
            $this->show_message('edit_successed');
        }
    }

    function payment_setting() {
		$model_setting = &af('settings');
        $setting = $model_setting->getAll(); //载入系统设置数据
        $mall_config_list = $this->_mall_config_mod->find(array('conditions' => 'parent_id = 2',));
        foreach ($mall_config_list as $key => $mall_config) {
            $setting[$mall_config['code']] = $mall_config['value'];
        }
        if (!IS_POST) {
            $this->assign('setting', $setting);
            $this->display('app_setting.payment_setting.html');
        } else {
            $data['app_alipay_partner_id'] = trim($_POST['app_alipay_partner_id']);
            $data['app_alipay_public_key'] = trim($_POST['app_alipay_public_key']);
            $data['app_alipay_private_key'] = trim($_POST['app_alipay_private_key']);
            
            if ($data['app_alipay_partner_id'] != $setting['app_alipay_partner_id']) {
                $this->_mall_config_mod->edit('code="app_alipay_partner_id"', array('value' => $data['app_alipay_partner_id']));
            }
            if ($data['app_alipay_public_key'] != $setting['app_alipay_public_key']) {
                $this->_mall_config_mod->edit('code="app_alipay_public_key"', array('value' => $data['app_alipay_public_key']));
            }
            if ($data['app_alipay_private_key'] != $setting['app_alipay_private_key']) {
                $this->_mall_config_mod->edit('code="app_alipay_private_key"', array('value' => $data['app_alipay_private_key']));
            }
            $data['app_wx_appid'] = trim($_POST['app_wx_appid']);
            $data['app_wx_appsecret'] = trim($_POST['app_wx_appsecret']);
            $data['app_wx_mchid'] = trim($_POST['app_wx_mchid']);
            $data['app_wx_key'] = trim($_POST['app_wx_key']);
            if ($data['app_wx_appid'] != $setting['app_wx_appid']) {
                $this->_mall_config_mod->edit('code="app_wx_appid"', array('value' => $data['app_wx_appid']));
            }
            if ($data['app_wx_appsecret'] != $setting['app_wx_appsecret']) {
                $this->_mall_config_mod->edit('code="app_wx_appsecret"', array('value' => $data['app_wx_appsecret']));
            }
            if ($data['app_wx_mchid'] != $setting['app_wx_mchid']) {
                $this->_mall_config_mod->edit('code="app_wx_mchid"', array('value' => $data['app_wx_mchid']));
            }
            if ($data['app_wx_key'] != $setting['app_wx_key']) {
                $this->_mall_config_mod->edit('code="app_wx_key"', array('value' => $data['app_wx_key']));
            }
			$model_setting->setAll($data);
            $this->show_message('edit_successed');
        }
    }
    
    

}
